// 题目描述
// https://leetcode.cn/problems/find-the-index-of-the-large-integer/

function ArrayReader(arr) {
  this.arr = Array.from(arr)

  this.compareSub = function (l, r, x, y) {
    if (this.arr[l] + this.arr[r] === this.arr[x] + this.arr[y]) {
      return 0
    }
    if (this.arr[l] + this.arr[r] > this.arr[x] + this.arr[y]) {
      return 1
    }
    if (this.arr[l] + this.arr[r] < this.arr[x] + this.arr[y]) {
      return -1
    }
  }

  this.length = function () {
    return this.arr.length
  }
}

/**
 * @param {ArrayReader} reader
 * @return {number}
 */
var getIndex = function (reader) {
  let y = reader.length() - 1
  let l = 0
  let r = Math.floor((l + y) / 2)
  let x = r + 1

  while (r <= x && l <= r && x <= y) {
    const res = reader.compareSub(l, r, x, y)
    if (res === 0) {
      --r
      --x
    } else if (res === 1) {
      y = r
      r = Math.floor((l + y) / 2)
      x = r + 1
    } else if (res === -1) {
      l = x
      r = Math.floor((l + y) / 2)
      x = r + 1
    }
  }

  return r
}

const reader = new ArrayReader([7, 1234, 7, 7, 7, 10, 7, 7, 7])

console.log(
  getIndex(reader)
)












